package com.jsits.v2x.common.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 转换工具类
 */
public class ConvertUtils
{
	/*
	 * 安全的String转Int
	 */
	public static final int parseInt(String number, int defaultValue)
	{
		int ret = 0;
		if (StringUtils.isBlank(number))
		{
			ret = defaultValue;
		} else
		{
			try
			{
				ret = Integer.parseInt(number);
			} catch (NumberFormatException e)
			{
				ret = defaultValue;
			}
		}

		return ret;
	}

	/*
	 * 安全的String转long
	 */
	public static final long parseLong(String number, long defaultValue)
	{
		long ret = 0;
		if (StringUtils.isBlank(number))
		{
			ret = defaultValue;
		} else
		{
			try
			{
				ret = Long.parseLong(number);
			} catch (NumberFormatException e)
			{
				ret = defaultValue;
			}
		}

		return ret;
	}
	
	/*
	 * 安全的String转Float
	 */
	public static final float parseFloat(String number, float defaultValue)
	{
		float ret = 0;
		if (StringUtils.isBlank(number))
		{
			ret = defaultValue;
		} else
		{
			try
			{
				ret = Float.parseFloat(number);
			} catch (NumberFormatException e)
			{
				ret = defaultValue;
			}

		}

		return ret;
	}

	/*
	 * 安全的String转Float
	 */
	public static final float parseFloat(String number)
	{
		float ret = 0;
		if (StringUtils.isBlank(number))
		{
			ret = 0;
		} else
		{
			try
			{
				ret = Float.parseFloat(number);
			} catch (NumberFormatException e)
			{
				ret = 0;
			}
		}

		return ret;
	}
}
